package dame;

import java.util.LinkedList;
import java.util.Queue;

public class MyStack2 {    //用队列实现栈
    public Queue<Integer> qu1;
    public Queue<Integer> qu2;

    public MyStack2() {
        qu1  =new LinkedList<>();
        qu2  =new LinkedList<>();
    }

    public void push(int x) {  //相当于入栈
        if(qu1.isEmpty()){
            qu1.offer(x);
        }else if(qu2.isEmpty()){
            qu2.offer(x);
        }
        qu1.offer(x);
    }

    public int pop() {  //想当于出栈
        if(empty()){
            return-1;
        }
        if(!qu1.isEmpty()){
            int size = qu1.size();
            for(int i=0;i<size-1;i++){
                qu2.offer(qu1.poll());
            }
            return qu1.poll();
        }else{
            int size = qu1.size();
            for(int i=0;i<size-1;i++){
                qu1.offer(qu2.poll());
            }
            return qu2.poll();
        }
    }

    public int top() {  //相当于打印栈顶元素
        if(empty()){
            return-1;
        }
        if(!qu1.isEmpty()){
            int size = qu1.size();
            int val = 0;
            for(int i=0;i<size;i++){
                val = qu1.poll();
                qu2.offer(val);
            }
            return val;
        }else{
            int size = qu2.size();
            int val = 0;
            for(int i=0;i<size;i++){
                val = qu2.poll();
                qu1.offer(val);
            }
            return val;
        }
    }

    public boolean empty() {  //判断是否为空
        return qu1.isEmpty()&&qu2.isEmpty();
    }
}
